﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using UnityEditor;
using UnityEngine;


namespace Elvex
{
    public enum AssetDirectoryImportOption
    {
        None, // 无处理
        Group, // 分组
    }

    public static class AssetsImporter
    {
        public static bool ImportAssetDictory(string path, AssetDirectoryImportOption option)
        {
            if (!AssetDatabase.IsValidFolder(path))
            {
                Debug.LogErrorFormat("AssetPath {0} is Invalid.", path);
                return false;
            }
            var guids = AssetDatabase.FindAssets(null, new string[] { path });
            foreach (var guid in guids)
            {
                var assetpath = AssetDatabase.GUIDToAssetPath(guid);
                // 筛选非文件夹资源
                if (AssetDatabase.IsValidFolder(assetpath))
                {
                    continue;
                }
                AssetImporter ai = AssetImporter.GetAtPath(assetpath);
                if (null == ai)
                {
                    Debug.LogErrorFormat("{0}资源不存在", assetpath);
                    return false;
                }

                switch (option)
                {
                    case AssetDirectoryImportOption.Group:
                        ai.assetBundleName = GetAssetBundleNameFromAssetDirectory(Path.GetDirectoryName(ai.assetPath));
                        break;
                    case AssetDirectoryImportOption.None:
                        ai.assetBundleName = GetAssetBundleNameFromAssetDirectory(path);
                        break;
                    default:
                        Debug.LogErrorFormat("ImportOption {0}不存在", option);
                        return false;
                }
                //ai.assetBundleVariant = "";
            }
            AssetDatabase.Refresh();
            Debug.LogFormat("Import AssetDictory({0}) Success.", path);
            return true;
        }

        public static bool ImportAssetDictory(string path)
        {
            return ImportAssetDictory(path, AssetDirectoryImportOption.Group);
        }

        const string m_assetsSymbol = "Assets";

        private static string GetAssetBundleNameFromAssetDirectory(string assetpath)
        {
            // 调用前会检查路径合法性 接口内就不做检查了 
            return assetpath.Substring(m_assetsSymbol.Length + 1).Replace("\\", "/").ToLower();
        }
        
        public static bool SaveAssetsSetting(string path)
        {
            string fullpath = Application.dataPath + path.Substring(6);
            if (!AssetDatabase.IsValidFolder(path))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(fullpath));
            }

            //AssetSettingConfiguration configuration = new AssetSettingConfiguration();
            //var guids = AssetDatabase.FindAssets("*", new string[]{ "Assets" });
            //foreach(var guid in guids)
            //{
            //    AssetImporter ai = AssetImporter.GetAtPath(AssetDatabase.GUIDToAssetPath(guid));
            //    if (null != ai && !string.IsNullOrEmpty(ai.assetBundleName))
            //    {
            //        if (ai.assetBundleName != GetAssetBundleNameFromAssetDirectory(Path.GetDirectoryName(ai.assetPath)))
            //        {
            //            configuration.AddAssetSetting(ai.assetPath, ai.assetBundleName);
            //        }
            //    }
            //}

            //configuration.Save(fullpath);
            //AssetDatabase.Refresh();
            //Debug.LogFormat("SaveAssetsSetting {0}", path);
            return true;
        }
    }
}
